package com.api.ld.web;


import com.engine.ld.functions.sap.newKhzsj.ZSD_CUSTOMERDATA_MAINTAINProxy;
import com.engine.ld.optimize.Khzsj;
import com.engine.ld.utils.DealXml;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Map;

//import weaver.general.Util;

/**
 * @author ZhangLei
 * @date 2021/6/30
 * 客户主数据
 * 将数据传给SAP接口并接收其返回的参数
 */
public class KhzsjAction implements Action {

    private static final BaseBean bb = new BaseBean();


    @Override
    public String execute(RequestInfo requestInfo) {
        bb.writeLog("Step0----------");
        String requestid = requestInfo.getRequestid();
        User user = requestInfo.getRequestManager().getUser();//退回时需要
        bb.writeLog("Step000000----------");
        return KhzsjHandle(requestid, user);
    }

    private String KhzsjHandle(String requestid, User user){
        String back = "";
        Khzsj khzsj = new Khzsj();
        bb.writeLog("----------");
//        String normlXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
//                "\t\t\t<ROOT>\n" +
//                "\t\t\t\t<GROUP>\n" +
//                "\t\t\t\t\t<KTOKD>Z001</KTOKD>\n" +
//                "\t\t\t\t\t<KUNNR>0000001006</KUNNR>\n" +
//                "\t\t\t\t\t<TITLE>0003</TITLE>\n" +
//                "\t\t\t\t\t<NAME1>测试客户1</NAME1>\n" +
//                "\t\t\t\t\t<NAME2>TEST01</NAME2>\n" +
//                "\t\t\t\t\t<NAME3>测试0100001</NAME3>\n" +
//                "\t\t\t\t\t<NAME4>TEST0100001</NAME4>\n" +
//                "\t\t\t\t\t<SORT1>测试</SORT1>\n" +
//                "\t\t\t\t\t<SORT2>TEST</SORT2>\n" +
//                "\t\t\t\t\t<STRAS>中国江苏昆山0001号</STRAS>\n" +
//                "\t\t\t\t\t<HOUSE_NUM1>89757</HOUSE_NUM1>\n" +
//                "\t\t\t\t\t<POST_CODE1>123456</POST_CODE1>\n" +
//                "\t\t\t\t\t<CITY1>昆山</CITY1>\n" +
//                "\t\t\t\t\t<COUNTRY>CN</COUNTRY>\n" +
//                "\t\t\t\t\t<REGION>100</REGION>\n" +
//                "\t\t\t\t\t<TIME_ZONE>UTC+8</TIME_ZONE>\n" +
//                "\t\t\t\t\t<LANGU>1</LANGU>\n" +
//                "\t\t\t\t\t<TELF1>4876543</TELF1>\n" +
//                "\t\t\t\t\t<TELF2>13888888888</TELF2>\n" +
//                "\t\t\t\t\t<FAX_NUMBER>KHANPOLLMANNCN</FAX_NUMBER>\n" +
//                "\t\t\t\t\t<SMTP_ADDR>KHAN@POLLMANN.CN</SMTP_ADDR>\n" +
//                "\t\t\t\t\t<LIFNR>B000134</LIFNR>\n" +
//                "\t\t\t\t\t<BRSCH>1000</BRSCH>\n" +
//                "\t\t\t\t\t<BRAN1/>\n" +
//                "\t\t\t\t\t<STCEG>310228000000000</STCEG>\n" +
//                "\t\t\t\t\t<BANKS>CN</BANKS>\n" +
//                "\t\t\t\t\t<BANKN>9024135022559</BANKN>\n" +
//                "\t\t\t\t\t<BANKL>B00278</BANKL>\n" +
//                "\t\t\t\t\t<IBAN>CN89070155300006882</IBAN>\n" +
//                "\t\t\t\t\t<KUKLA>2</KUKLA>\n" +
//                "\t\t\t\t\t<ZFLAG>1</ZFLAG>\n" +
//                "\t\t\t\t\t<LOEVM/>\n" +
//                "\t\t\t\t</GROUP>\n" +
//                "\t\t\t\t<COMPANY>\n" +
//                "\t\t\t\t\t<KUNNR>0000001006</KUNNR>\n" +
//                "\t\t\t\t\t<BUKRS>1010</BUKRS>\n" +
//                "\t\t\t\t\t<AKONT>0011220101</AKONT>\n" +
//                "\t\t\t\t\t<ZUAWA>000</ZUAWA>\n" +
//                "\t\t\t\t\t<ZTERM>X040</ZTERM>\n" +
//                "\t\t\t\t\t<TOGRU>0001</TOGRU>\n" +
//                "\t\t\t\t\t<ZWELS>B</ZWELS>\n" +
//                "\t\t\t\t\t<LOEVM/>\n" +
//                "\t\t\t\t\t<ZFLAG>1</ZFLAG>\n" +
//                "\t\t\t\t</COMPANY>\n" +
//                "\t\t\t\t<SALE>\n" +
//                "\t\t\t\t\t<KUNNR>0000001006</KUNNR>\n" +
//                "\t\t\t\t\t<VKORG>1011</VKORG>\n" +
//                "\t\t\t\t\t<VTWEG>10</VTWEG>\n" +
//                "\t\t\t\t\t<SPART>10</SPART>\n" +
//                "\t\t\t\t\t<BZIRK>101017</BZIRK>\n" +
//                "\t\t\t\t\t<VKBUR>1110</VKBUR>\n" +
//                "\t\t\t\t\t<VKGRP>110</VKGRP>\n" +
//                "\t\t\t\t\t<KDGRP>54</KDGRP>\n" +
//                "\t\t\t\t\t<KONDA/>\n" +
//                "\t\t\t\t\t<KALKS>1</KALKS>\n" +
//                "\t\t\t\t\t<PLTYP/>\n" +
//                "\t\t\t\t\t<VERSG>1</VERSG>\n" +
//                "\t\t\t\t\t<LPRIO>02</LPRIO>\n" +
//                "\t\t\t\t\t<VSBED>1</VSBED>\n" +
//                "\t\t\t\t\t<VWERK>1011</VWERK>\n" +
//                "\t\t\t\t\t<KZAZU>X</KZAZU>\n" +
//                "\t\t\t\t\t<ANTLF>9</ANTLF>\n" +
//                "\t\t\t\t\t<WAERS>CNY</WAERS>\n" +
//                "\t\t\t\t\t<ZTERM>X040</ZTERM>\n" +
//                "\t\t\t\t\t<INCO1>CIF</INCO1>\n" +
//                "\t\t\t\t\t<INCO2>成本, 保险 &amp; 运费</INCO2>\n" +
//                "\t\t\t\t\t<KTGRD>10</KTGRD>\n" +
//                "\t\t\t\t\t<ALAND>CN</ALAND>\n" +
//                "\t\t\t\t\t<TATYP>MWST</TATYP>\n" +
//                "\t\t\t\t\t<TAXKD>1</TAXKD>\n" +
//                "\t\t\t\t\t<ZKTONR_BP/>\n" +
//                "\t\t\t\t\t<ZKTONR_PY/>\n" +
//                "\t\t\t\t\t<ZKTONR_SH/>\n" +
//                "\t\t\t\t\t<LOEVM/>\n" +
//                "\t\t\t\t\t<ZFLAG>1</ZFLAG>\n" +
//                "\t\t\t\t</SALE>\n" +
//                "\t\t\t</ROOT>";
        try {
            bb.writeLog("Step2----------");


//            function01
//            ZSD_CUSTOMERDATA_MAINTAIN_BindingStub bindingStub = new ZSD_CUSTOMERDATA_MAINTAIN_BindingStub();
            bb.writeLog("Step3----------");
//            String SAPBackXml = bindingStub.ZSD_CUSTOMERDATA_MAINTAIN(khzsj.create(requestid));
//            ERROR:no ending point


//            function02
//            ZSD_CUSTOMERDATA_MAINTAIN_Service ll = new ZSD_CUSTOMERDATA_MAINTAIN_ServiceLocator();
//            String SAPBackXml = ll.getZSD_CUSTOMERDATA_MAINTAIN().ZSD_CUSTOMERDATA_MAINTAIN(khzsj.create(requestid));
//            ERROR:java.net.UnknownHostException:lemqas.lemtech.cn

//            No.3
            ZSD_CUSTOMERDATA_MAINTAINProxy pp = new ZSD_CUSTOMERDATA_MAINTAINProxy();
            String SAPBackXml = pp.getZSD_CUSTOMERDATA_MAINTAIN_PortType().ZSD_CUSTOMERDATA_MAINTAIN(khzsj.create(requestid));
//            硬代码测试开始
//            String SAPBackXml = pp.getZSD_CUSTOMERDATA_MAINTAIN_PortType().ZSD_CUSTOMERDATA_MAINTAIN(normlXml);
//            bb.writeLog("传给SAP的xml参数是："+normlXml);
//            ERROR:java.net.UnknownHostException:lemqas.lemtech.cn


            bb.writeLog("SAP的返回结果是："+SAPBackXml);
            Map<String, Object> sapBackMap = new DealXml().XmlResultMap(SAPBackXml);
            bb.writeLog("sapBackMap解析xml的返回结果是"+sapBackMap);

            bb.writeLog("Step5----------");
            String type = sapBackMap.get("TYPE").toString();
            String message  = sapBackMap.get("MESSAGE").toString();
            message = message.contains("'")?message.replace("'","''"):message;

            bb.writeLog("type:"+type+";message:"+message);
//            将sap返回结果存入数据库
            boolean saptodatabase = saptodatabase(type, message,requestid);
            bb.writeLog("SAP返回结果添加到数据库的结果："+saptodatabase);
            if ("S".equals(type)){
//                back = "SUCCESS";
                back = Action.SUCCESS;
            }else {
//                back = "FALSE";
                bb.writeLog("发现错误执行了该方法"+message);

                try{
//                    new WorkflowBackUtil().rejectRequest(user, Util.getIntValue(requestid), false, -1, -1);
                    back =  Action.FAILURE_AND_CONTINUE;
                    bb.writeLog("返回值back是："+back);
                }catch (Exception e){
                    bb.writeLog("流程无法归档，重新提交出现错误，错误信息为："+e);

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            bb.writeLog("错误信息为："+e);
        }

        bb.writeLog("方法结束后back的值为："+back);
        return back;
//        return "0";
    }


    /*将sap返回的结果存入数据库*/
    private boolean saptodatabase(String type, String message,String requestid){
        String sql = "UPDATE formtable_main_86 SET TYPE='"+type+"',MESSAGE='"+message+"' WHERE requestId="+requestid;
        RecordSet rs = new RecordSet();
        return rs.executeUpdate(sql);
    }

}
